package hashtable;

/**
 * @Author wangj
 * @Date 2022/4/26 22:07
 */
public class EmpLinkedListArray {

    private EmpLinkedList[] empLinked;
    private int size;

    public EmpLinkedListArray(int size){
        this.size = size;
        empLinked = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            empLinked[i] = new EmpLinkedList();
        }
    }

    public void add(Emp emp){
        int arrayNo = hashFun(emp.id);
        empLinked[arrayNo].add(emp);
    }

    public void list(){
        for (int i = 0; i < size; i++) {
            empLinked[i].list(i);
        }
    }

    public Emp findEmpById(int id){
        int empListNo = hashFun(id);
        Emp emp = empLinked[empListNo].findEmpId(id);
        System.out.printf(" id=%d,name=%s", emp.id, emp.name);
        System.out.println();
        return emp;
    }

    public int hashFun(int id){
        return id % size;
    }
}
